package CCRMClient.GUI;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Component;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;

import java.awt.SystemColor;

import javax.swing.JTextField;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.ClientCustomerManagement;
import Common.SERVER_COMMANDS;
import Common.Entities.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.sql.Date;

/**
 * This class implements Update Customer screen
 * @author Ola
 *
 */
public class UpdateCustomerScreen  extends JPanel {
	private JLabel lblUpdateCustomer;
	private JLabel lblCustomerID;
	private JLabel lblCustmerName;
	private JLabel lblCustomerPhone;
	private JLabel lblCustomerAddress;
	private JLabel lblCustomerRegion;
	private JTextField textCustomerID;
	private JTextField textCustomerName;
	private JTextField textCustomerPhone;
	private JTextField textCustomerAddress;
	private JTextField textCustomerRegion; 
	private JButton btnUpdateCustomer;
	private JButton btnCancel;

	/**
	 * Create add customer panel
	 */
	public UpdateCustomerScreen() {

		initialize();
	}
	
	
	private void initialize(){
		setSize (565,549);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblUpdateCustomer());
		add(getLblCustomerID());
		add(getLblCustomerName());
		add(getLblCustomerPhone());
		add(getLblCustomerAddress());	
		add(getLblCustomerRegion());
		add(getTextCustomerID());
		add(getTextCustomerName());
		add(getTextCustomerPhone());
		add(getTextCustomerAddress());
		add(getTextCustomerRegion());
		//add(getComCustomerRegion());
		add(getBtnUpdateCustomer());
		add(getBtnCancel());
}
	
	JLabel getLblUpdateCustomer(){
		if(lblUpdateCustomer==null){
			lblUpdateCustomer=new JLabel("Update Customer");
			lblUpdateCustomer.setHorizontalAlignment(SwingConstants.CENTER);
			lblUpdateCustomer.setForeground(Color.RED);
			lblUpdateCustomer.setFont(new Font("David", Font.BOLD, 26));
			lblUpdateCustomer.setBackground(Color.WHITE);
			lblUpdateCustomer.setBounds(0, 13, 565, 39);
		}
		return lblUpdateCustomer;
}

	JLabel getLblCustomerID(){
		
		if(lblCustomerID==null){
			lblCustomerID=new JLabel("Customer ID");
			lblCustomerID.setFont(new Font("David", Font.BOLD, 20));
			lblCustomerID.setBounds(30, 80, 164, 29);
		}
		return lblCustomerID;
		
	}
	
	JLabel getLblCustomerName(){
		if(lblCustmerName==null){
			lblCustmerName= new JLabel("Customer Name");
			lblCustmerName.setFont(new Font("David", Font.BOLD, 20));
			lblCustmerName.setBounds(30, 142, 164, 29);
		}
			
		
		return lblCustmerName;
		
	}
	JLabel getLblCustomerPhone(){
		
		
		if(lblCustomerPhone == null){
			lblCustomerPhone=new JLabel("Customer Phone");
			lblCustomerPhone.setFont(new Font("David", Font.BOLD, 20));
			lblCustomerPhone.setBounds(30, 207, 164, 29);
		}
		return lblCustomerPhone;
		
	}
	

	JLabel getLblCustomerAddress() {
		if(lblCustomerAddress == null){
			lblCustomerAddress=new JLabel("Customer Address");
			lblCustomerAddress.setFont(new Font("David", Font.BOLD, 20));
			lblCustomerAddress.setBounds(30, 267, 164, 29);
		}
		
		return lblCustomerAddress;
	}
	
	JLabel getLblCustomerRegion() {
		if(lblCustomerRegion == null){
			lblCustomerRegion=new JLabel("Customer Region");
			lblCustomerRegion.setFont(new Font("David", Font.BOLD, 20));
			lblCustomerRegion.setBounds(30, 328, 164, 29);
			}
		
		
		return lblCustomerRegion ;
	}

	JTextField getTextCustomerID() {
		if (textCustomerID == null) {
			textCustomerID = new JTextField();
			textCustomerID.setColumns(10);
			textCustomerID.setBackground(SystemColor.menu);
			textCustomerID.setBounds(237, 85, 251, 20);
		}
		
		return textCustomerID ;
	}
	JTextField getTextCustomerName() {
		if (textCustomerName == null) {
			textCustomerName = new JTextField();
			textCustomerName.setColumns(10);
			textCustomerName.setBackground(SystemColor.menu);
			textCustomerName.setBounds(237, 147, 251, 20);
		}
		
		
		return textCustomerName;
	}
	JTextField getTextCustomerPhone() {
		if (textCustomerPhone == null) {
			textCustomerPhone = new JTextField();
			textCustomerPhone.setColumns(10);
			textCustomerPhone.setBackground(SystemColor.menu);
			textCustomerPhone.setBounds(237, 212, 251, 20);
		}
		return textCustomerPhone;
	}
	
	JTextField getTextCustomerRegion() {
		if (textCustomerRegion == null) {
			textCustomerRegion = new JTextField();
			textCustomerRegion.setColumns(10);
			textCustomerRegion.setBackground(SystemColor.menu);
			textCustomerRegion.setBounds(237, 333, 251, 20);
		}
		
		return textCustomerRegion;
	}
	
	JTextField getTextCustomerAddress() {
		if (textCustomerAddress == null) {
			textCustomerAddress = new JTextField();
			textCustomerAddress.setColumns(10);
			textCustomerAddress.setBackground(SystemColor.menu);
			textCustomerAddress.setBounds(237, 272, 251, 20);
		}
		
		return textCustomerAddress;
	}
	
	JButton getBtnUpdateCustomer(){
		if (btnUpdateCustomer == null) {
			btnUpdateCustomer = new JButton("Update Customer");
			btnUpdateCustomer.addActionListener(new BntUpdateCustomerActionListener());
			btnUpdateCustomer.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnUpdateCustomer.setBounds(60, 460, 195, 23);
		}
		return btnUpdateCustomer;
		
	}
	
	JButton getBtnCancel(){
		if (btnCancel == null) {
			btnCancel = new JButton("Cancel");
			btnCancel.addActionListener(new BtnCancelActionListener());
			btnCancel.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnCancel.setBounds(289, 460, 230, 23);
	}
		return btnCancel;
		
	}
	/**
	 * This inner class implements event listener for pressing cancel button
	 * @author Ola
	 *
	 */
	private class BtnCancelActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new CustomerManage());
		}
  }
	/**
	 * This inner class implements event listener for pressing update customer button
	 * @author Ola
	 *
	 */
	 private class BntUpdateCustomerActionListener implements ActionListener{
		 public void actionPerformed(ActionEvent arg0) {
			 try{
				String CustomerID=textCustomerID.getText();
				String CustomerName=textCustomerName.getText();
				String customerPhone=textCustomerPhone.getText();
				String customerAddress=textCustomerAddress.getText();
				int customerStatus=1;
				int regionID = 0;
				try {
					regionID = Integer.parseInt(textCustomerRegion.getText());
				} catch (NumberFormatException e3) {
					JOptionPane.showMessageDialog(null, "Invalid region ID input", "Update customer", JOptionPane.INFORMATION_MESSAGE);
					textCustomerRegion.setText("");
					throw new NullPointerException();
				}
				 String str_date="1900-10-10";
				 java.sql.Date customerBrithday = java.sql.Date.valueOf(str_date);
				 if (CustomerID.isEmpty()||CustomerName.isEmpty()||customerPhone.isEmpty()||customerAddress.isEmpty()) throw new NullPointerException();
				 try{
				 Customer cust= new Customer(CustomerID,CustomerName,customerBrithday,customerPhone,customerAddress,customerStatus,regionID);
				 ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.UPDATE_CUSTOMER,cust);}
				 catch(IOException e) {
						JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
						ClientStart.quitApp();
				 }
			 }catch (NullPointerException e) {
					JOptionPane.showMessageDialog(null, "All fields are mandatory", "Update customer problem", JOptionPane.INFORMATION_MESSAGE);
			 }
			 
			 
		 
	 }
}
}
	 

